 // Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
 // Jad home page: http://www.kpdus.com/jad.html
 // Decompiler options: packimports(3) 
 // Source File Name:   Grid.java

 package com.jb.web.bean;

 import com.jb.framework.PersistentContext;
 import com.jb.util.ClassUtil;
 import com.jb.web.application.EventManager;
 import com.jb.web.bean.event.CellChangeEvent;
 import com.jb.web.context.WebContext;
 import com.jb.web.model.GUID;
 import java.io.StringReader;
 import java.util.*;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import org.w3c.dom.*;
 import org.xml.sax.InputSource;

 // Referenced classes of package com.jb.web.bean:
 //            HtmlItem, TableRow, TableCell, TableHeader, 
 //            TableFooter

 public class Grid extends HtmlItem
 {

     private void $init$()
     {
         columns = 5;
     }

     public void setColumns(int pColumns)
     {
         columns = pColumns;
     }

     public int getColumns()
     {
         return columns;
     }

     public void setDefault(PersistentContext pCtx)
     {
         setStyleClass(DEFAULT_CLASS);
         setRows(5);
         setShowHeader(true);
         valueChanged(pCtx);
     }

     public TableFooter getFooter(PersistentContext pCtx)
     {
         if(footerRow == null)
         {
             List list = getChildrenByType(TableFooter.class);
             if(list.size() == 0)
             {
                 createFooter(pCtx);
             } else
             {
                 footerRow = (TableFooter)list.get(0);
                 footerRow.setId((new StringBuilder()).append(getId()).append("Footer").toString());
             }
         }
         return footerRow;
     }

     public TableHeader getHeader(PersistentContext pCtx)
     {
         if(headerRow == null)
         {
             List list = getChildrenByType(TableHeader.class);
             if(list.size() == 0)
             {
                 createHeader(pCtx);
             } else
             {
                 headerRow = (TableHeader)list.get(0);
                 headerRow.setId((new StringBuilder()).append(getId()).append("Header").toString());
             }
         }
         return headerRow;
     }

     public TableRow getRow(int loc)
     {
         List rows = getChildrenByClass(TableRow.class);
         if(loc < rows.size())
             return (TableRow)rows.get(loc);
         else
             return null;
     }

     public void setRows(int pRows)
     {
         if(pRows != rows)
             rows = pRows;
     }

     public int getRows()
     {
         return rows;
     }

     public void setShowFooter(boolean showFooter)
     {
         this.showFooter = showFooter;
     }

     public boolean isShowFooter()
     {
         return showFooter;
     }

     public void setShowHeader(boolean showHeader)
     {
         this.showHeader = showHeader;
     }

     public boolean isShowHeader()
     {
         return showHeader;
     }

     public void onPaste(Object pasted)
     {
         if(pasted instanceof TableRow)
             rows++;
     }

     public boolean valueChanged(PersistentContext pCtx)
     {
         List list = getChildrenByClass(TableRow.class);
         if(list.size() < rows)
         {
             for(int i = list.size(); i < rows; i++)
             {
                 TableRow row = (TableRow)ClassUtil.createInstance(TableRow.class);
                 row.setId(GUID.getInstance().next(TableRow.class));
                 row.setSequence((i + 1) * 10);
                 addChild(row);
                 row.setDefault(pCtx);
             }

         } else
         {
             int newsize = list.size() - rows;
             if(newsize > 0)
             {
                 TableRow row;
                 for(int i = rows; i < list.size(); removeChild(row))
                 {
                     row = (TableRow)list.get(i);
                     i++;
                 }

             } else
             {
                 TableRow row;
                 for(int i = 0; i < list.size(); row.valueChanged(pCtx))
                 {
                     row = (TableRow)list.get(i);
                     i++;
                 }

             }
         }
         getHeader(pCtx).valueChanged(pCtx);
         getFooter(pCtx).valueChanged(pCtx);
         return true;
     }

     private void createFooter(PersistentContext pCtx)
     {
         footerRow = new TableFooter();
         footerRow.setStyleClass("jb_table_footer");
         footerRow.setId((new StringBuilder()).append(getId()).append("F").toString());
         footerRow.setClientId((new StringBuilder()).append(getId()).append("_f").toString());
         addChild(footerRow);
         footerRow.setDefault(pCtx);
     }

     public void setEditable(boolean param)
     {
         editable = param;
     }

     public boolean isEditable()
     {
         return editable;
     }

     private void createHeader(PersistentContext pCtx)
     {
         headerRow = new TableHeader();
         headerRow.setStyleClass("jbTableHeader");
         headerRow.setId((new StringBuilder()).append(getId()).append("H").toString());
         headerRow.setClientId((new StringBuilder()).append(getId()).append("_h").toString());
         addChild(headerRow);
         headerRow.setDefault(pCtx);
     }

     public void setAutoUpdate(boolean param)
     {
         autoUpdate = param;
     }

     public boolean isAutoUpdate()
     {
         return autoUpdate;
     }

     public void setCellValues(WebContext ctx, String xmlValues)
     {
         String values[];
         String cellDetails[];
         for(Iterator iter = parseCellData(xmlValues).iterator(); iter.hasNext(); setCellValue(ctx, Integer.parseInt(cellDetails[2]), Integer.parseInt(cellDetails[1]), values[1]))
         {
             values = (String[])iter.next();
             cellDetails = values[0].split("_");
         }
     }

     protected void setCellValue(WebContext ctx, int col, int row, String value)
     {
         boolean changeValueAllowed = true;
         if(onCellChange != null)
         {
             CellChangeEvent event = new CellChangeEvent(this, col, row, value);
             EventManager.getInstance().handleEvent(ctx, this, onCellChange, event);
             changeValueAllowed = event.isChangeAllowed();
         }
         if(changeValueAllowed)
         {
             TableCell cell = getRow(row).getCellAt(col);
             if(cell != null)
                 cell.setValue(value);
         }
     }

     static List parseCellData(String xmlValues)
     {
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         dbf.setNamespaceAware(true);
         List list = new ArrayList();
         dbf.setValidating(false);
         try
         {
             DocumentBuilder db = dbf.newDocumentBuilder();
             Document doc = db.parse(new InputSource(new StringReader(xmlValues)));
             Node xmlElement = doc.getDocumentElement();
             for(Node child = xmlElement.getFirstChild(); child != null; child = child.getNextSibling())
                 list.add(new String[] {
                     child.getAttributes().getNamedItem("name").getNodeValue(), child.getFirstChild().getNodeValue()
                 });

             List list1 = list;
             return list1;
         }
         catch(Exception e)
         {
             e.printStackTrace();
         }
         return Collections.emptyList();
     }

     public void setOnCellChange(String param)
     {
         onCellChange = param;
     }

     public String getOnCellChange()
     {
         return onCellChange;
     }

     public Grid()
     {
         $init$();
     }

     static final long serialVersionUID = 0x1e0f3L;
     private static String DEFAULT_CLASS = "jbTable";
     private TableFooter footerRow;
     private TableHeader headerRow;
     private boolean showFooter;
     private boolean showHeader;
     private boolean editable;
     private boolean autoUpdate;
     private int columns;
     private int rows;
     private String onCellChange;

     static 
     {
         publish(Grid.class, "Event Handler", "onCellChange");
         publish(Grid.class, "Style", "width");
         publish(Grid.class, "Style", "height");
     }
 }
